package com.example.myapplication.cutvideo.handler;

import android.graphics.Bitmap;

import com.example.myapplication.cutvideo.IProviderExpand;
import com.example.myapplication.cutvideo.util.BitmapUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : yangzhi.ou
 *     e-mail : xxx@xx
 *     time   : 2019/05/26
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class BitmapProvider implements IProviderExpand<Bitmap> {
    private final List<String> adapter;
    private int index = 0;

//    private Queue<byte[]> queue;

    private int[] scaleSize;

    public BitmapProvider(ArrayList<String> adapter, int[] scaleSize) {
        this.adapter = adapter;
        this.scaleSize = scaleSize;
//        queue = new LinkedList<>();

//        for (int i = 0; i < adapter.size(); i++) {
//            if (i < adapter.size()) {
//                Bitmap bitmap = genBitmap(i);
//                queue.add(BitmapUtil.getBytesByPNG(bitmap));
//                bitmap.recycle();
//            }
//        }
    }

    @Override
    public void prepare() {
    }

    @Override
    public void finish() {
    }

    @Override
    public void finishItem(Bitmap item) {
//        item.recycle();
//        System.gc();
    }

    @Override
    public boolean hasNext() {
        return index < adapter.size();
    }

    @Override
    public int size() {
        return adapter.size();
    }

//    private Bitmap genBitmap(int index) {
//        return adapter.get(index);
//        return BitmapUtil.decodeSampleBitmapFromResource(path, scaleSize[0], scaleSize[1]);
//    }

    @Override
    public Bitmap next() {
//        return adapter.get(index);
//        byte[] bytes = queue.poll();

//        long time1 = System.currentTimeMillis();
        Bitmap bitmap = BitmapUtil.decodeSampleBitmapFromResource(adapter.get(index), scaleSize[0], scaleSize[1]);
//        byte[] bytes = BitmapUtil.getBytesByPNG(bitmap);
//        System.out.println("ssss time " + (System.currentTimeMillis() - time1));
//        Bitmap bitmap1 = BitmapUtil.loadFromBytesByPNG(bytes);
        index++;
        return bitmap;
    }
}
